package com.itheima.ticket4;


import java.util.concurrent.locks.ReentrantLock;

public class Ticket implements Runnable {
    private int count = 100;
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        //卖票
        while (true) {
            //获得锁   加锁
            try {
                lock.lock();
                if (count <= 0) {
                    break;
                } else {
                    Thread.sleep(100);

                    String name = Thread.currentThread().getName();
                    count--;
                    System.out.println(name + "卖了一张票,剩余" + count + "张票");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //释放锁
                lock.unlock();
            }

        }
    }
}
